<template>
	<view>
		<view>
			<uni-list>
				<uni-list-item :title="order['ordertime']" :rightText="order['order_list'][0]['orderstate']"
					:note="order['shippingaddr']">
				</uni-list-item>

			</uni-list>
			<view>
				<view @click="getClicked(0)">订单详细</view>
				<view v-if="showMore">
					<view>
						<view @click="getClicked(1)">商品信息</view>
						<view v-if="showGoods">
							<view v-for="(goods,key) in goods_list" :key="key" style="margin-bottom: 10px;">
								<view>编号：{{goods['goodsid']}}</view>
								<view>名称：{{goods['goodsname']}}</view>
								<view>类别：{{goods['goodscategory']}}</view>
								<view>规格：{{goods['goodsstandard']}}</view>
								<view>价格：{{goods['goodsprice']}}</view>
								<view class="img-box">
									<view v-if="goods['goodspicture'].length==0">
										<image src="../../../../static/default_goods.png" class="addpic"></image>
									</view>
									<view v-if="goods['goodspicture'].length!=0&&isList"
										v-for="img in goods['goodspicture']">
										<image :src="img"></image>
									</view>
								</view>
								<view v-if="userType===1">库存：{{goods['goodsstock']}}</view>
								<view>购买数量：{{goods['purchasenum']}}</view>
								<view>小计：￥{{Number(goods['purchasenum'])*Number(goods['goodsprice'])}}</view>
								<view></view>
							</view>
						</view>
					</view>
					<view v-if="userType===1">
						<view @click="getClicked(2)">顾客信息</view>
						<view v-if="showCus">
							<view>
								<uni-lst>
									<uni-list-item :title="customer.cusname" :note="customer.addrshipping"
										:thumb="customer.cuswxicon">
										<template slot="footer" @click="getClicked(customer.custelenumber)">
											<!-- <uni-icons type="arrowright" color="#8F8F94"></uni-icons> -->
										</template>
									</uni-list-item>
									<uni-list-item title="联系用户">
										<template slot="footer">
											<image class="img" src="@/static/tele.png"
												@click="makeCall(customer.custelenumber)"></image>
											<img class="img" src="@/static/wechat.png" @tap="sendWXmsg">
										</template>
									</uni-list-item>
								</uni-lst>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view>总计：￥{{order.totalmoney}}</view>
			<view style="height: 30px;"></view>
			<view class="change_order" @click="changeOrderState"
				v-if="(userType===1&&orderstate==='已订购')||(userType===0&&orderstate==='配送中')">修改订单状态</view>
		</view>
		<!-- 遍历对象 -->
		<!-- <view v-for="(key,value,index) in obj"></view> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				order: [],
				oldorders: [],
				showMore: false,
				showGoods: false,
				showCus: false,
				goods_list: [],
				customer: {},
				src: '../../../../../static/default_goods.png',
				isList: false,
				orderstate: ''
			}
		},
		computed: mapState(['hasLogin', 'userName', 'userType']),
		methods: {
			getClicked(pos) {
				if (pos === 0) {
					this.showMore = !this.showMore;
					console.log(this.showMore);
				} else if (pos === 1) {
					this.showGoods = !this.showGoods;
				} else {
					this.showCus = !this.showCus;
					this.customer = this.order.customerinfo;
				}
			},
			makeCall(phoneNumber) {
				uni.makePhoneCall({
					phoneNumber: phoneNumber,
					success() {
						console.log("call");
					}
				})
			},
			sendWXmsg() {
				console.log("send a msg");
			},
			changeOrderState() {
				let order_list = this.order.order_list;
				let id = order_list[0].orderid;
				this.$myRequest({
					url: '/assets/change_order_state/',
					data: {
						'id': id,
						'type': this.userType
					}
				}).then(res => {
					console.log(res);
					let showText = '';
					if (this.userType == 1) {
						showText = '订单已发出'
					} else {
						showText = '已确认收货'
					}
					uni.showToast({
						title: showText,
						icon: 'success',
						success: () => {
							setTimeout(function() {
								uni.navigateBack({
									delta: 1
								})
							}, 1000)
						}
					})
				})
			}

		},
		onLoad(option) {
			let orderid = option.orderid;
			console.log(orderid)
			console.log('onload');
			this.$myRequest({
				url: '/assets/get_order_detail_byid/',
				data: {
					"id": Number(orderid)
				}
			}).then(res => {
				console.log(res.data)
				this.order = res.data;
				let order_list = this.order.order_list;
				this.orderstate = order_list[0].orderstate;
				this.goods_list = this.order.goodsinfo;
				let index;
				for (index = 0; index < this.goods_list.length; index++) {
					let item = this.goods_list[index]
					this.$set(this.goods_list[index], 'purchasenum', order_list[index]['purchasenum'])
					this.$myRequest({
						url: '/product/get_pict_url/',
						data: {
							"id": item['goodsid']
						}
					}).then(res => {
						if (res.data.length === 0) {
							item.goodspicture = []
						} else {
							item.goodspicture = res.data;
							if (Object.prototype.toString.call(res.data) === '[object Object]') {
								this.isList = true;
							}
						}
					})
				}
				console.log(this.goods_list);
			})
		}
	}
</script>

<style>
	.change_order {
		position: fixed;
		bottom: 0;
		height: 60rpx;
		line-height: 60rpx;
		width: 100%;
		text-align: center;
		background: yellow;
	}
</style>
